unit uMeetingDM;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uAbstractDataModule, FMTBcd, DBClient, Provider, DB, SqlExpr;

type
  TMeetingDM = class(TAbstractDataModule)
    SqlControlMEETING_ID: TIntegerField;
    SqlControlTOPIC: TStringField;
    SqlControlDURATION: TTimeField;
    SqlControlSTARTDATE: TDateField;
    SqlControlSTARTTIME: TTimeField;
    SqlControlUSER_ID: TIntegerField;
    SqlControlUSER_NAME: TStringField;
    SqlControlROOM_ID: TIntegerField;
    SqlControlROOM_NAME: TStringField;
    SqlControlLASTCHANGE: TDateField;
    cdsControlMEETING_ID: TIntegerField;
    cdsControlTOPIC: TStringField;
    cdsControlDURATION: TTimeField;
    cdsControlSTARTDATE: TDateField;
    cdsControlSTARTTIME: TTimeField;
    cdsControlUSER_ID: TIntegerField;
    cdsControlUSER_NAME: TStringField;
    cdsControlROOM_ID: TIntegerField;
    cdsControlROOM_NAME: TStringField;
    cdsControlLASTCHANGE: TDateField;
    sqlParticipants: TSQLDataSet;
    dspParticipants: TDataSetProvider;
    cdsParticipants: TClientDataSet;
    cdsParticipantsMEETING_ID: TIntegerField;
    cdsParticipantsUSER_ID: TIntegerField;
    sqlSearchPart: TSQLDataSet;
    dspSearchPart: TDataSetProvider;
    cdsSearchPart: TClientDataSet;
    sqlSearchPartUSER_ID: TIntegerField;
    sqlSearchPartUSER_NAME: TStringField;
    cdsSearchPartUSER_ID: TIntegerField;
    cdsSearchPartUSER_NAME: TStringField;
    sqlParticipantsMEETING_ID: TIntegerField;
    sqlParticipantsUSER_ID: TIntegerField;
    sqlParticipantsUSER_NAME: TStringField;
    cdsParticipantsUSER_NAME: TStringField;
    procedure dspControlBeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet;
      DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
      var Applied: Boolean);
    procedure cdsControlNewRecord(DataSet: TDataSet);
    procedure cdsControlAfterScroll(DataSet: TDataSet);
    procedure dspControlAfterUpdateRecord(Sender: TObject; SourceDS: TDataSet;
      DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind);
    procedure cdsControlBeforeEdit(DataSet: TDataSet);
    procedure cdsControlBeforePost(DataSet: TDataSet);
    procedure cdsControlDURATIONChange(Sender: TField);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MeetingDM: TMeetingDM;

implementation
uses uMeetingControl,uUserControl;
{$R *.dfm}

procedure TMeetingDM.cdsControlAfterScroll(DataSet: TDataSet);
begin
  inherited;
  cdsSearchPart.Close;
  cdsSearchPart.Params.ParamByName('MEETING_ID').AsInteger:=
    DataSet.FieldByName('MEETING_ID').AsInteger;
  cdsSearchPart.Open;

  cdsParticipants.Close;
  cdsParticipants.Params.ParamByName('MEETING_ID').AsInteger:=
    DataSet.FieldByName('MEETING_ID').AsInteger;
  cdsParticipants.Open;
end;

procedure TMeetingDM.cdsControlBeforeEdit(DataSet: TDataSet);
begin
  if not TMeetingControl.GetInstance.CheckMeetingOwner then
    raise Exception.Create('This operation is granted to the meeting owner only!');
  inherited;
end;

procedure TMeetingDM.cdsControlBeforePost(DataSet: TDataSet);
begin
  inherited;
  CheckRequiredsFields(DataSet);

  if not TMeetingControl.GetInstance.CheckNumberOfParts then
    raise Exception.Create('At least two participants must be selected!');

  if DataSet.State=dsInsert then
    begin
      DataSet.FieldByName('MEETING_ID').AsInteger:=GenerateID('GEN_MEETING_ID');
      cdsParticipants.First;
      while not cdsParticipants.Eof do
      begin
        cdsParticipants.Edit;
        cdsParticipants.FieldByName('MEETING_ID').AsInteger:=
          DataSet.FieldByName('MEETING_ID').AsInteger;
        cdsParticipants.Post;
        cdsParticipants.Next;

      end;
    end;
    DataSet.FieldByName('LASTCHANGE').AsDateTime:=Now;
end;

procedure TMeetingDM.cdsControlDURATIONChange(Sender: TField);
begin
  inherited;
  if cdsControl.State in [dsInsert,dsEdit] then
  begin
    cdsControl.FieldByName('STARTTIME').AsString:='';
    cdsControl.FieldByName('ROOM_ID').AsInteger;
    cdsControl.FieldByName('ROOM_NAME').AsString:='';
  end;
end;

procedure TMeetingDM.cdsControlNewRecord(DataSet: TDataSet);
begin
  inherited;
  cdsControl.FieldByName('USER_ID').AsInteger:=TUserControl.GetInstance.fUserID;
end;

procedure TMeetingDM.dspControlAfterUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind);
begin
  inherited;
  if UpdateKind=ukInsert then
    cdsParticipants.ApplyUpdates(0);
end;

procedure TMeetingDM.dspControlBeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
  var Applied: Boolean);
begin
  inherited;
  if SourceDS.Name='sqlControl' then
  begin
    if not TMeetingControl.GetInstance.ValidateMeetingRoom(
      DeltaDS.FieldByName('MEETING_ID').AsInteger,
      DeltaDS.FieldByName('ROOM_ID').AsInteger,
      DeltaDS.FieldByName('STARTDATE').AsDateTime,
      DeltaDS.FieldByName('STARTTIME').AsDateTime,
      DeltaDS.FieldByName('DURATION').AsInteger) then
        raise Exception.Create('There is no more avaliability for this room!');

        cdsParticipants.First;
        while not cdsParticipants.Eof do
        begin
          if True then
            if not TMeetingControl.GetInstance.ValidateMeetingRoom(
              DeltaDS.FieldByName('MEETING_ID').AsInteger,
              cdsParticipants.FieldByName('USER_ID').AsInteger,
              DeltaDS.FieldByName('STARTDATE').AsDateTime,
              DeltaDS.FieldByName('STARTTIME').AsDateTime,
              DeltaDS.FieldByName('DURATION').AsInteger) then
                raise Exception.Create('There is no more availability for user '+
                cdsParticipants.FieldByName('USER_NAME').AsString+'!');

                TMeetingControl.GetInstance.SendMeetingNotification(
                  cdsParticipants.FieldByName('USER_ID').AsInteger,
                      DeltaDS.FieldByName('STARTDATE').AsDateTime,
                      DeltaDS.FieldByName('STARTTIME').AsDateTime,
                      DeltaDS.FieldByName('DURATION').AsInteger,
                      DeltaDS.FieldByName('ROOM_NAME').AsString,
                      DeltaDS.FieldByName('TOPIC').AsString,
                      Integer(UpdateKind));

              if UpdateKind=ukDelete then
                cdsParticipants.Delete
              else
                cdsParticipants.Next;
              end;
          if UpdateKind in [ukModify,ukDelete] then
              cdsParticipants.ApplyUpdates(0);

        end;

  end;

end.
